package metier.outils;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Graphics;
import java.util.LinkedList;

import metier.modele.Figure;

/**
 * Figure => Main levée
 * @author Quentin, Vincent, Charlie
 *
 */
public class Tools_Main extends Figure {

	
	private LinkedList<Point> list;
	private static final long serialVersionUID = -956535916480296341L;

	/**
	 * Constructeur
	 */
	public Tools_Main() {
		list = new LinkedList<Point>();
	}


	public void setDonnees(int x1, int y1, int x2, int y2) {
		list.addLast(new Point(x2, y2));
	}

	public void setX2(int I) {
	}
	public void setY2(int I) {
	}

	public int getX2() {
		return 0;
	}
	public int getY2() {
		return 0;
	}
	
	public Point getTopLeftCoords()
	{
		Point point = new Point(Integer.MAX_VALUE, Integer.MAX_VALUE);
		for (Point p : list) {
			if (p.x < point.x)
				point.x = p.x;
			if (p.y < point.y)
				point.y = p.y;
		}
		return point;
	}
	public Point getBotRightCoords()
	{
		Point point = new Point(Integer.MIN_VALUE, Integer.MIN_VALUE);
		for (Point p : list) {
			if (p.x > point.x)
				point.x = p.x;
			if (p.y > point.y)
				point.y = p.y;
		}
		return point;
	}
	
	
	/**
	 * Ajout du rectangle de suppression
	 * @param g
	 */
	public void addDeleteRect(Graphics2D g)
	{
		int offset = Tools.EDIT_RECT_SIZE;
		float epaisseur=1;
		float[] style = {10,5};
		Point p1 = getTopLeftCoords();
		Point p2 = getBotRightCoords();
		System.out.println(p1.x+","+p1.y+ " "+p2.x+","+p2.y+ " ");
		g.setStroke( new BasicStroke( 
		        epaisseur,
		        BasicStroke.CAP_BUTT,
		        BasicStroke.JOIN_MITER,
		        10.0f,
		        style,
		        0
		        ));
		
		g.drawRect(p1.x - offset, p1.y - offset, Math.abs(p2.x - p1.x) + 2*offset, Math
				.abs(p2.y - p1.y) + 2*offset);
		
		
		g.setStroke( new BasicStroke(1.0f) );
	}

	/**
	 * Pour dessiner le résultat
	 */
	public void tracer(Graphics g, boolean couleursVraies, boolean edit,
			Color color) {

		Color cfg;

		if (couleursVraies == true) {
			cfg = getFg();
		} else {
			cfg = color;
		}

		g.setColor(cfg);

		for (int i = 1; i < list.size(); i++) {
			g.drawLine((int) ((Point) (list.get(i - 1))).getX(),
					(int) ((Point) (list.get(i - 1))).getY(),
					(int) ((Point) (list.get(i))).getX(), (int) ((Point) (list
							.get(i))).getY());
		}
		
		if (getStatus() == Figure.SUPPRESSION)
			addDeleteRect((Graphics2D) g);
	}

	/**
	 * Est-ce que le point 'p' est sur la figure ?
	 */
	public boolean pointOnFigure(Point p) {
		Point temp;
		for (int i = 0; i < list.size(); i++) {
			temp = list.get(i);
			if (isAroundPoint(p, temp) == true)
				return true;
		}
		return false;
	}

	public int pointInEditFigure(Point p) {
		return 0;
	}

	public boolean pointInFigure(Point p) {
		return pointOnFigure(p);
	}

	/**
	 * Est-ce que le point 'p2' est proche du point 'p' ?
	 * @param p
	 * @param p2
	 * @return
	 */
	private boolean isAroundPoint(Point p, Point p2) {

		int borne = Tools.MIN_DISTANCE;

		for (int x = -borne; x < borne; x++) {
			for (int y = -borne; y < borne; y++) {
				if (p.x == p2.x + x && p.y == p2.y + y)
					return true;
			}
		}
		return false;
	}

	public LinkedList<Point> getList() {
		return list;
	}

}